{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85628725-c58b-451f-b524-ae76580fdb2d",
   "metadata": {},
   "source": [
    "# doit\n",
    "\n",
    "> _The use of `doit` is an implementation detail, and is subject to change!_\n",
    "\n",
    "Under the hood, the [CLI](../reference/cli.ipynb) is powered by\n",
    "[doit](https://github.com/pydoit/doit), a lightweight task engine in python comparable\n",
    "to `make`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3d39984-e025-4796-a8b3-e6333d4d4f39",
   "metadata": {},
   "source": [
    "## Using Tasks with the API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32963cce-1e92-48e7-9b03-21cd4ba7aced",
   "metadata": {
    "tags": [
     "hide-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import atexit\n",
    "import os\n",
    "import pathlib\n",
    "import shutil\n",
    "import tempfile\n",
    "\n",
    "from IPython.display import *\n",
    "\n",
    "if \"TMP_DIR\" not in globals():\n",
    "    TMP_DIR = pathlib.Path(tempfile.mkdtemp(prefix=\"_my_lite_dir_\"))\n",
    "\n",
    "    def clean():\n",
    "        shutil.rmtree(TMP_DIR)\n",
    "\n",
    "    atexit.register(clean)\n",
    "os.chdir(TMP_DIR)\n",
    "print(pathlib.Path.cwd())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b6951f6-1c91-4bd1-a848-f30dd382d8ad",
   "metadata": {},
   "source": [
    "The `LiteManager` collects all the tasks from _Addons_, and can optionally accept a\n",
    "`task_prefix` in case you need to integrate with existing tasks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85b387a1-e068-4860-8d66-d61010924703",
   "metadata": {},
   "outputs": [],
   "source": [
    "from jupyterlite_core.manager import LiteManager\n",
    "\n",
    "manager = LiteManager(task_prefix=\"lite_\")\n",
    "manager.initialize()\n",
    "manager.doit_run(\"lite_status\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dae619b7-4816-414c-a715-7667fceed670",
   "metadata": {},
   "source": [
    "## Custom Tasks and `%doit`\n",
    "\n",
    "`doit` offers an IPython\n",
    "[magic](https://ipython.readthedocs.io/en/stable/interactive/magics.html), enabled with\n",
    "an extension. This can be combined to create highly reactive build tools for creating\n",
    "very custom sites."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94057df0-eef0-4f21-abf3-ed49864c0af7",
   "metadata": {},
   "outputs": [],
   "source": [
    "%reload_ext doit"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddcc37f4-a9c6-4212-a7a0-96b17ce4ce09",
   "metadata": {},
   "source": [
    "It works against the `__main__` namespace, which won't have anything by default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "334f8e33-d587-4a1a-8e02-2ec475460e64",
   "metadata": {},
   "outputs": [],
   "source": [
    "%doit list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df90ff08-ebce-4468-a3cc-19af7956ac69",
   "metadata": {},
   "source": [
    "All the JupyterLite tasks can be added by updating `__main__` via `globals`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3d33ebe-9621-458a-87d6-f67f64eeb43a",
   "metadata": {},
   "outputs": [],
   "source": [
    "globals().update(manager._doit_tasks)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf00f8a2-a7ff-489d-bdc5-c8bab3775fa2",
   "metadata": {},
   "source": [
    "Now when a new task is created, it can reference other tasks and targets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f982ccb-96c8-47f0-9103-0db4f65790e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def task_hello():\n",
    "    return dict(actions=[lambda: print(\"HELLO!\")], task_dep=[\"lite_post_status\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2069e569-8957-4567-a254-9ea12e08a2a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "%doit -v2 hello"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
